<?php
namespace Modules\Order\Http\Controllers\Client\V1;

use Illuminate\Http\Request;
use Illuminate\Routing\Controller;

use Modules\Order\Entities\ProductOrder as Order;
use Modules\Order\Entities\Aftersale;

use Validator;
use ThrowException;
use Auth;
use Setting;

class AftersaleController extends Controller
{
    public function show(Request $req, $uuid)
    {
        $user = Auth::requireLoginUser();

        $aftersale = Aftersale::getEntity([
            'uuid' => $uuid,
            'with' => 'order.skus'
        ]);

        $only = [
            'id',
            'uuid',
            'number',
            'deliver_status',
            'reason',
            'desc',
            'images',
            'refund_money',
            'refund_score',
            'audit_remark',
            'sendback_info',
            'audited_at',
            'sendback_at',
            'refunded_at',
            'completed_at',
            'union_status',
            'union_status_text',
            'order' => [
                'id',
                'uuid',
                'number',
                'type',
                'score_used',
                'product_money_price',
                'product_score_price',
                'union_status',
                'union_status_text',
                'skus' => [
                    '*' => [
                        'id',
                        'uuid',
                        'title',
                        'number',
                        'thumb',
                        'attrs',
                        'money_price',
                        'score_price',
                        'discount_money_price',
                        'discount_score_price',
                        'total'
                    ]
                ]
            ]
        ];

        $info = $aftersale->getInfo($only);

        return [
            'info' => $info
        ];
    }


    public function store(Request $req)
    {
        $rule = [
            'order_id' => ['required'],
            'deliver_status' => ['required']
            // 'address_id' => ['integer'],
            // 'coupon_id' => ['integer']
        ];

        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        // 订单
        $order = Order::getEntity([
            'user_id' => $user->id,
            'id' => $req->order_id
        ]);

        if (!$order->isAftersaleable()) {
            ThrowException::Conflict('此订单状态暂不可申请售后~');
        }

        // 创建售后
        $options = $req->all();
        $item = Aftersale::createWith($order, $options);

        return [
            'id' => $item->id,
            'uuid' => $item->uuid
        ];
    }

    public function update(Request $req, $uuid)
    {
        $rule = [
            'type' => ['required'],
        ];

        Validator::make($req->all(), $rule)->fails() && ThrowException::BadRequest();

        $user = Auth::requireLoginUser();

        // 订单
        $aftersale = Aftersale::getEntity([
            'user_id' => $user->id,
            'uuid' => $uuid
        ]);

        $type = $req->type;

        // 更新寄回信息
        if ($type === 'sendback') {
            $info = $req->input('info');
            $aftersale->sendback($info);
        }
        elseif ($type === 'close') {
            $aftersale->closeByUser();
        }

        return [];
    }
}
